---------------------------- Aimbot code ---------------------------------
function SlobTeamAllowed(ent)
	if GetConVarNumber("wots_aimbot_teammode") >= 1 then
		if ent:Team() != minge:Team() then
			return true
		else
			return false
		end
	end
	return true
end

function EzekBuddyAllowed(ent)
	if ent:IsNPC() then 
		return true
	end
	if GetConVarNumber("wots_buddy_attack") >= 0 then 
		return true
	end
	if GetConVarString("wots_buddy_1") == ent:Nick() or
	GetConVarString("wots_buddy_2") == ent:Nick() or
	GetConVarString("wots_buddy_3") == ent:Nick() or
	GetConVarString("wots_buddy_4") == ent:Nick() or 
	GetConVarString("wots_buddy_5") == ent:Nick() then
		return false
	end
	return true
end

function SlobBotAllowed(ent)
	if !ent:IsValid() or !ent:IsNPC() and !ent:IsPlayer() or minge == ent then return false end
	if ent:IsPlayer() and !ent:Alive() then return false end
	if ent:IsPlayer() and string.find(string.lower(team.GetName(ent:Team())), "spectator") then return false end
	if ent:IsPlayer() and !SlobTeamAllowed(ent) then return false end
	if ent:IsNPC() and ent:GetMoveType() == 0 then return false end
	return true
end

function HeadPosition(ent)
	local hbone = ent:LookupBone("ValveBiped.Bip01_Head1")
	return ent:GetBonePosition(hbone) + Vector(0, 0, GetConVarNumber("wots_aimbot_add")) + EzekPerfectOffset(ent)
end

function GetSlobBotTarget()
	local position = minge:EyePos()
	local angle = minge:GetAimVector()
	local tar = {0,0}
	if GetConVarNumber("wots_attackto") == 1 then
		for _, ent in pairs(player.GetAll()) do
			if SlobBotAllowed(ent) and BotVisible(ent) and EzekBuddyAllowed(ent) then
				local targetpos = ent:EyePos()
				local difr = (targetpos-position):Normalize()
				difr = difr - angle
				difr = difr:Length()
				difr = math.abs(difr)
				if difr < tar[2] or tar[1] == 0 then
					tar = {ent, difr}
				end
			end
		end
	elseif GetConVarNumber("wots_attackto") == 2 then
		for _, ent in pairs(ents.GetAll()) do
			if ent:IsNPC() and ent:GetMoveType() != 0 and BotVisible(ent) then
				local targetpos = ent:EyePos()
				local difr = (targetpos-position):Normalize()
				difr = difr - angle
				difr = difr:Length()
				difr = math.abs(difr)
				if difr < tar[2] or tar[1] == 0 then
					tar = {ent, difr}
				end
			end
		end
	elseif GetConVarNumber("wots_attackto") == 3 then
		for _, ent in pairs(ents.GetAll()) do
			if SlobBotAllowed(ent) and BotVisible(ent) and EzekBuddyAllowed(ent) then
				local targetpos = ent:EyePos()
				local difr = (targetpos-position):Normalize()
				difr = difr - angle
				difr = difr:Length()
				difr = math.abs(difr)
				if difr < tar[2] or tar[1] == 0 then
					tar = {ent, difr}
				end
			end
		end
	end
	return tar[1]
end

_G.SafeAIM = _R["CUserCmd"].SetViewAngles

function SlobMegaAIM(UCMD)
	if aiming then
		local pwned = GetSlobBotTarget()
		if pwned != 0 then
			local targetaim = (HeadPosition(pwned) - minge:GetShootPos()):Angle()
			SafeAIM(UCMD, targetaim)
		end
		local tr = minge:GetEyeTrace()
		if SlobBotAllowed(tr.Entity) and EzekBuddyAllowed(tr.Entity) then
			if GetConVarNumber("wots_aimbot_mouselock") >= 1 then
				RunConsoleCommand("cl_mouseenable", "0")
			end
			if GetConVarNumber("wots_aimbot_autoattack") >= 1 then
				local aad = GetConVarNumber("wots_aimbot_autoattack_delay")
				if !timer.IsTimer("autoattack") then
					timer.Create("autoattack", aad, 0, function()
						RunConsoleCommand("wots_attack") -- Autoattack
					end)
				end
			end
		else
	        if GetConVarNumber("cl_mouseenable") == 0 then
		        RunConsoleCommand("cl_mouseenable", "1")
            end
			if timer.IsTimer("autoattack") then
				timer.Destroy("autoattack")
			end
		end
	end
end

hook.Add("CreateMove", "MingeBagAIMBot", SlobMegaAIM)

concommand.Add("wots_attack", function()
    LocalPlayer():ConCommand("+attack; wait 2; +attack; wait 2; -attack")
end)

concommand.Add("+slobpos", function()
    FakeView = minge:EyeAngles()
    aiming = true
end)

concommand.Add("-slobpos", function()
    aiming = false

	if GetConVarNumber("wots_aimbot_mouselock") >= 1 then
		RunConsoleCommand("cl_mouseenable", "1")
    end
    if timer.IsTimer("autoattack") then
	    timer.Destroy("autoattack")
	end
end)
-------------------------------------------------------------